
import 'package:flutter/material.dart';

const Color _defaultTitleColor = const Color.fromARGB(255, 93, 100, 110);
const Color _defaultTitleColorLight = const Color.fromARGB(255, 247, 247, 247);
const Color _defaultDetailColor = const Color.fromARGB(255, 133, 140, 150);
const Color _defaultDetailColorLight = const Color.fromARGB(255, 218, 218, 218);

class TLEmptyContainer extends StatefulWidget {

  TLEmptyContainer({
    Key key,
    this.backgroundColor,
    this.customLoadingWidget,
    this.showLoading = false,
    this.image,
    this.title,
    this.titleStyle,
    this.detailText,
    this.detailTextStyle,
    this.space = 8.0,
    this.actionButton,
  }) : super(key : key);

  final Color backgroundColor;
  final Widget customLoadingWidget;
  final bool  showLoading;
  final Image image;
  final String title;
  final TextStyle titleStyle;
  final String detailText;
  final TextStyle detailTextStyle;
  final double space;

  final Widget actionButton;

  @override
  State<TLEmptyContainer> createState() {
    // TODO: implement createState
    return TLEmptyContainerState();
  }
}

class TLEmptyContainerState extends State<TLEmptyContainer> {

  void _addChildAndSpacingIfNeeded(List<Widget> list, Widget newChild) {
    if(list.length > 0 ) {
      list.add(SizedBox(height: widget.space,));
    }
    list.add(newChild);
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    final isDarkMode = Theme.of(context).brightness == Brightness.dark;
    final List<Widget> children = <Widget>[];
    if(widget.customLoadingWidget != null) {
      _addChildAndSpacingIfNeeded(children, widget.customLoadingWidget);
    }

    if(widget.customLoadingWidget == null && widget.showLoading) {
      Widget loading = CircularProgressIndicator(
        strokeWidth: 3.0,
        valueColor: AlwaysStoppedAnimation(Theme.of(context).accentColor),
      );
      _addChildAndSpacingIfNeeded(children, loading);
    }

    if(widget.image != null) {
      _addChildAndSpacingIfNeeded(children, widget.image);
    }

    if(widget.title != null && widget.title.isNotEmpty) {
      TextStyle textStyle = widget.titleStyle ?? TextStyle(
        fontSize: 16.0,
        color: isDarkMode ? _defaultTitleColorLight : _defaultTitleColor
      );
      Widget title = Text(widget.title, style: textStyle, textAlign: TextAlign.center,);
      _addChildAndSpacingIfNeeded(children, title);
    }

    if(widget.detailText != null && widget.detailText.isNotEmpty) {
      TextStyle detailTextStyle = widget.detailTextStyle ?? TextStyle(
        fontSize: 14.0,
        color: isDarkMode ? _defaultDetailColorLight : _defaultDetailColor,
      );
      Widget detail = Text(widget.detailText, style: detailTextStyle, textAlign: TextAlign.center,);
      _addChildAndSpacingIfNeeded(children, detail);
    }

    if(widget.actionButton != null) {
      _addChildAndSpacingIfNeeded(children, widget.actionButton);
    }

    return Container(
      color: widget.backgroundColor,
      padding: EdgeInsets.all(40.0),
      child: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: children,
        ),
      ),
    );
  }
}